<template>
  <div class="main"
       v-loading="loading"
       element-loading-text="拼命加载中"
       element-loading-spinner="el-icon-loading"
  >

    <el-empty v-if="!loading && !data.length" description="暂无数据"></el-empty>

    <el-form label-width="150px">
      <el-form-item v-for="(attr, i) in data" :key="i" :label="attr.attrName">
        <el-checkbox-group v-model="attr.values">
          <template v-if="attr.valueSelect">
            <el-checkbox v-for="(value, index) in attr.valueSelect.split(',')" :key="index" :label="value"
            ></el-checkbox>
            <div style="margin-right: 20px; display: inline"></div>
          </template>
          <div style="display:inline">
            <el-button
              v-show="!attr.inputView"
              @click="showInput(i, attr)"
              size="mini"
            >+ 自定义
            </el-button>
            <el-input
              v-show="attr.inputView"
              v-model="attr.input"
              size="mini"
              :ref="'input' + i"
              style="width:150px"
              @keyup.enter.native="handleInputConfirm(attr, false)"
              @blur="handleInputConfirm(attr, true)"
            ></el-input>
          </div>
        </el-checkbox-group>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
export default {
  name: 'pubSale',
  data() {
    return {
      data: [],
      remember: undefined,
      loading: false
    }
  },
  methods: {
    init(catId) {
      if (catId) {
        // 页面缓存，只有 catId 变了才发送请求
        if (!this.remember || this.remember !== catId) {
          this.loading = true
          this.data = []
          this.$api.get('/product/attr/saleByCatId/' + catId).then(({ data }) => {
            // 赋默认值
            data.forEach(i => {
              i.values = []
              i.inputView = false
              i.input = undefined
            })
            this.data = data
            this.remember = catId
            this.loading = false
          })
        }
      } else {
        this.data = []
        this.remember = undefined
      }
    },
    showInput(index, attr) {
      attr.inputView = true
      this.$nextTick(() => {
        // input 获取焦点
        this.$refs['input' + index][0].focus()
      })
    },
    handleInputConfirm(attr, blur) {
      if (attr.input && '' !== attr.input.trim()) {
        if (attr.valueSelect && '' !== attr.valueSelect.trim()) {
          attr.valueSelect += ',' + attr.input
        } else {
          attr.valueSelect = attr.input
        }
      }
      attr.input = undefined
      if (blur) {
        attr.inputView = false
      }
    },
    getForm() {
      let sales = []
      let index = 0
      this.data.forEach((item) => {
        if(item.values && item.values.length) {
          sales[index++] = {
            attrId: item.attrId,
            attrName: item.attrName,
            attrValue: item.values
          }
        }
      })
      return sales
    },
    // 表单校验
    validate() {
      return this.getForm().length
    }
  }
}
</script>

<style scoped>
.main {
  margin: 30px 70px 50px 70px;
}
</style>
